package ac.svn.accesscontrol.utils;

import java.awt.Color;
import java.awt.Component;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.RadialGradientPaint;
import java.awt.RenderingHints;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Point2D;
import java.awt.geom.RoundRectangle2D;

import javax.swing.JComponent;
import javax.swing.border.Border;

import ac.notification.event.NotificationEvent;
import ac.notification.manager.NotificationRegistrar;
import ac.svn.accesscontrol.store.IConstants;
import ac.svn.accesscontrol.store.ISVNIdentifier;
import ac.svn.accesscontrol.ui.DropShadowBorder;

public class UIUtility 
{
	public static Color light = new Color(0xCAE1FF);//CAE1FF
	public static Color dark  = new Color(0xA2B5CD); //A2B5CD	

	public static Graphics getPainted(Graphics g, JComponent comp) {
		Graphics2D g2 = (Graphics2D) g;

		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

		GradientPaint p;
		p = new GradientPaint(0, 0, light,
				0, comp.getHeight()/4, dark);

		Paint oldPaint = g2.getPaint();
		g2.setPaint(p);

		g2.fill(new RoundRectangle2D.Double(0,0,comp.getWidth(),comp.getHeight(),5,5));


		g2.setColor(Color.GREEN);
		/*g2.setStroke(new BasicStroke(2));
	    g2.draw(new RoundRectangle2D.Double(0,0,getWidth()- 1,getHeight()-1,0,0));*/

		Paint radical = new RadialGradientPaint(new Point2D.Double(comp.getWidth() / 2.0,
				comp.getHeight()), comp.getWidth(),
				new float[] { 0.0f, 0.5f ,1.0f },
				new Color[] {light.brighter(),dark,dark.darker()});
		g2.setPaint(radical);

		g2.fill(new RoundRectangle2D.Double(0,0,comp.getWidth(),comp.getHeight(),5,5));


		g2.setPaint(oldPaint);

		return g2;
	}

	public static Border setAllShadowedBorder(JComponent component)
	{
		Border border = new DropShadowBorder(Color.BLACK,5,0.5f,5,true,true,true,true);
		if(component != null)
		{
			component.setBorder(border);
		}
		return border;

	}

	
	public static void addPopupListener(final Component component,final String popupType)
	{
		MouseAdapter mouseAdapter  =new MouseAdapter()
		{

			/* (non-Javadoc)
			 * @see java.awt.event.MouseAdapter#mouseReleased(java.awt.event.MouseEvent)
			 */
			@Override
			public void mouseReleased(MouseEvent e) 
			{
				if(e.isPopupTrigger())
				{
					NotificationEvent event = new NotificationEvent(ISVNIdentifier.CMD_SHOW_POPUP);
					event.addParameter(IConstants.KEY_POPUP_SOURCE, component);
					event.addParameter(IConstants.KEY_POPUP_TYPE, popupType);
					event.addParameter(IConstants.KEY_POPUP_EVENT, e);
					NotificationRegistrar.dispatchNotification(event);
				}
			}
		};
		
		component.addMouseListener(mouseAdapter);
		
	}

}
